package com.wbl.train.business.controller.admin;
import cn.hutool.core.util.ObjectUtil;
import com.wbl.train.business.service.StationService;
import com.wbl.train.common.business.dto.StationDto;
import com.wbl.train.common.business.entity.StationEntity;
import com.wbl.train.common.business.vo.StationVo;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.common.result.R;
import com.wbl.train.common.result.ResultCode;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王博纶
 * @since 2024-02-01
 * @description 车站
 */
@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/admin/station")
@Tag(name = "车站控制器类",
        description = "StationController 车站 后端数据接口")
public class StationAdminController {
    private final StationService stationService;
    /**
     * 通过id查询 车站
     * @param stationDto 车站Dto
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/query")
    public R<PageVo<StationEntity>> getById(StationDto stationDto) {
        // 车站查询
        log.debug("stationDto :{}",stationDto);
        PageVo<StationEntity> pageVo = stationService.queryStation(stationDto);
        return R.ok("查询成功",pageVo);
    }

    /**
     * 查询所有的车站信息
     * @return Result
     */
    @Operation(summary = "查询所有车站", description = "查询所有车站")
    @GetMapping("/query_all")
    public R<List<StationVo>> getAllTrainStation() {
        // 火车车次查询
        List<StationVo> stationVos = stationService.queryAllStation();
        return R.ok("查询成功",stationVos);
    }

    /**
     * 新增 车站
     * @param station 车站
     * @return Result
     */
    @Operation(summary = "新增车站", description = "新增车站")
    @PostMapping("/save")
    public R<Boolean> save(@Valid @RequestBody StationEntity station) {
        return R.ok("保存成功",stationService.insertAndUpdateStation(station));
    }

    /**
     * 通过id删除 车站
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除车站",
               description = "通过id删除车站")
    @DeleteMapping("/delete/{id}")
    public R<Boolean> removeById(@PathVariable Long id) {
        if(ObjectUtil.isNull(stationService.getById(id))){
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"当前车站信息不存在");
        }
        return R.ok(stationService.removeById(id));
    }
}
